#!/usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import datetime
from enum import IntEnum
import persistent
from typing import List, Dict, Any, Optional

class TestType(IntEnum):
    """测试类型枚举"""
    UI = 1
    UNIT = 2
    PERF = 3
    UNKNOWN = 4


class Test(persistent.Persistent):
    """基础测试类，包含测试的基本信息"""
    def __init__(self, id: int, name: str, owner: str, type: IntEnum):
        self.id = id
        self.name = name
        self.owner = owner
        self.type = type
        self.first_edit = datetime.now()
        self.last_edit = datetime.now()
        self.last_publish = datetime.now()

class UiTestStep(persistent.Persistent):
    """测试步骤类，描述单个测试步骤"""
    def __init__(self, action: str, target: str, params: Optional[Dict[str, Any]] = None, expected_result: str = ""):
        self.action = action  # 操作类型，如点击、输入、滑动等
        self.target = target  # 操作目标，如元素ID、坐标等
        self.params = params or {}  # 操作参数
        self.expected_result = expected_result  # 预期结果
        self.created_time = datetime.now()

class UiTestCase(Test):
    """UI自动化测试用例元数据类，包含测试用例的完整信息"""
    def __init__(self, id: int, name: str, owner: str, type: IntEnum, description: str):
        super().__init__(id, name, owner, type)
        self.description = description  # 测试用例描述
        self.steps: List[UiTestStep] = []  # 测试步骤列表
        # self.prerequisites: str = ""  # 前置条件
        # self.tags: List[str] = []  # 标签列表
        # self.status: str = "草稿"  # 状态：草稿、待执行、执行中、已完成
        # self.device_requirements: Dict[str, Any] = {}  # 设备要求
        # self.execution_history: List[Dict[str, Any]] = []  # 执行历史记录
        
    def add_step(self, step: UiTestStep) -> None:
        """添加测试步骤"""
        self.steps.append(step)
        self.last_edit = datetime.now()
        
    def update_status(self, status: str) -> None:
        """更新测试用例状态"""
        self.status = status
        self.last_edit = datetime.now()
        
    def add_execution_record(self, result: str, device_id: str, execution_time: datetime, 
                             duration: float, log_path: str = "") -> None:
        """添加执行记录"""
        record = {
            "result": result,  # 通过、失败、错误
            "device_id": device_id,
            "execution_time": execution_time,
            "duration": duration,  # 执行时长（秒）
            "log_path": log_path  # 日志文件路径
        }
        self.execution_history.append(record)
        self.last_edit = datetime.now()